﻿using Ebusiness_ProductService.Services.Dtos.Product;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Ebusiness_ProductService.Services;
using Volo.Abp.Application.Dtos;
using SkyWalking.NetworkProtocol.V3;
using DotNetCore.CAP;
using Servicecomb.Saga.Omega.Abstractions.Transaction;

namespace Ebusiness_ProductService.Controllers;


[Route("Product/[action]")]
[ApiController]
public class ProductController : AbpController, IProductService
{
    public IProductService productService { get; set; }

    public ProductController()
    {

    }

    ///// <summary>
    ///// 
    ///// </summary>
    ///// <param name="productDto"></param>
    ///// <returns></returns>
    //[HttpPost]
    //public async Task<ProductDto> Create(ProductDto productDto)
    //{
    //    //Console.WriteLine("当前线程 ID1 是: " + Thread.CurrentThread.ManagedThreadId);
    //    var dto = await productService.CreateAsync(productDto);
    //    //Console.WriteLine("当前线程 ID4 是: " + Thread.CurrentThread.ManagedThreadId);
    //    return dto;
    //}

    /// <summary>
    /// 获取列表数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<List<ProductDto>> GetList()
    {
        var dtos = await productService.GetListAsync(new PagedAndSortedResultRequestDto() { MaxResultCount = 50, SkipCount = 0 });
        return dtos.Items.ToList();
    }

    [HttpGet]
    public async Task<ProductDto> GetAsync(Guid id)
    {
        return await productService.GetAsync(id);
    }

    [HttpPost]
    public async Task<PagedResultDto<ProductDto>> GetListAsync(PagedAndSortedResultRequestDto input)
    {
        return await productService.GetListAsync(input);
    }

    /// <summary>
    /// Castle.Proxies.IApplicationServiceProxy
    /// Castle.Proxies.IApplicationServiceProxy
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [Compensable(nameof(DistributedDelete))] // 调用删除订单方法
    public async Task<ProductDto> CreateAsync(ProductDto input)
    {
        return await productService.CreateAsync(input);
    }

    /// <summary>
    /// 分布式事务 删除
    /// </summary>
    /// <param name="productDto"></param>
    /// <returns></returns>
    void DistributedDelete(ProductDto productDto)
    {
        productService.DeleteAsync(productDto.Id).Wait();
    }

    [HttpPut]
    public async Task<ProductDto> UpdateAsync(Guid id, ProductDto input)
    {
        return await productService.UpdateAsync(id, input);
    }

    [HttpDelete]
    public async Task DeleteAsync(Guid id)
    {
        await productService.DeleteAsync(id);
    }

    /// <summary>
    /// 接受添加商品事件的方法
    /// </summary>
    /// <param name="productDto"></param>
    /// <returns></returns>
    [NonAction]
    [CapSubscribe("AddProduct")] // 1、用来接收 添加商品事件
    public string GetAddProductEvent(ProductDto productDto)
    {
        // throw new Exception("添加数据库");
        // 2、存储商品数据
        //throw new Exception("");
        productDto = productService.CreateAsync(productDto).Result;

        return "接受成功";
    }
}
